package com.littleken.foodieApi.controller;

import com.littleken.foodieCommon.Response.ErrorCode;
import com.littleken.foodieCommon.Response.ServerResponse;
import com.littleken.foodiePojo.domain.ItemsDO;
import com.littleken.foodiePojo.domain.ItemsImgDO;
import com.littleken.foodiePojo.domain.ItemsParamDO;
import com.littleken.foodiePojo.domain.ItemsSpecDO;
import com.littleken.foodiePojo.vo.CommentLevelCountsVO;
import com.littleken.foodiePojo.vo.ItemCommentVO;
import com.littleken.foodiePojo.vo.ItemInfoVO;
import com.littleken.foodiePojo.vo.SearchItemsVO;
import com.littleken.foodieService.ItemService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * 商品 controller
 * Create by littleken on 2020/11/24 14:11
 */
@RestController
@RequestMapping("/items")
public class ItemsController {

    @Resource
    private ItemService itemService;

    @GetMapping("/getItemInfo/{itemId}")
    public ServerResponse<ItemInfoVO> getItemInfo(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return ServerResponse.fail(ErrorCode.PARAMS_INVALID);
        }
        ItemsDO itemsDO = itemService.getItemById(itemId);
        List<ItemsImgDO> itemImgList = itemService.getItemImgList(itemId);
        List<ItemsSpecDO> itemSpecList = itemService.getItemSpecList(itemId);
        ItemsParamDO itemParam = itemService.getItemParam(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO(itemsDO, itemImgList, itemSpecList, itemParam);
        return ServerResponse.success(itemInfoVO);
    }

    @GetMapping("/getCommentCounts/{itemId}")
    public ServerResponse<CommentLevelCountsVO> getCommentCounts(@PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return ServerResponse.fail(ErrorCode.PARAMS_INVALID);
        }
        CommentLevelCountsVO commentCounts = itemService.getCommentCounts(itemId);
        return ServerResponse.success(commentCounts);
    }

    @GetMapping("/getPagedComments/{itemId}/{level}")
    public ServerResponse<List<ItemCommentVO>> getPagedComments(@PathVariable String itemId,
                                                                @PathVariable Integer level) {
        List<ItemCommentVO> allComments = itemService.getAllComments(itemId, level);
        return ServerResponse.success(allComments);
    }

    /**
     * 根据关键词查询
     * keywords 关键字 sort 排序字段 k 关键字 c 销量 p价格
     *
     * @return
     */
    @GetMapping("/searchByKeywords/{keywords}/{sort}")
    public ServerResponse<List<SearchItemsVO>> searchByKeywords(@PathVariable String keywords,
                                                                @PathVariable String sort) {
        List<SearchItemsVO> searchItemsVOS = itemService.searhItemsByKeyword(keywords, sort);
        return ServerResponse.success(searchItemsVOS);
    }

    @GetMapping("/searchByCatId/{catId}/{sort}")
    public ServerResponse<List<SearchItemsVO>> searchByCatId(@PathVariable Integer catId,
                                        @PathVariable String sort) {
        List<SearchItemsVO> searchItemsVOS = itemService.searhItemsByCatId(catId, sort);
        return ServerResponse.success(searchItemsVOS);
    }

}
